import React, { useState, useEffect } from 'react';
import { connect } from 'react-redux';
import { history } from 'umi';
import { Button, Form, Input } from 'antd';
import timestampToTime from '../../hooks/data';
interface Props {}

function Bianji({ dispatch }: any) {
  const obj: any = history.location.state;
  const data = obj.state;
  data.price = `${data.price}¥`;
  const phone = JSON.parse(data.sale_phone).phone;
  data.sale_phone = phone ? phone : '暂无';
  const dataid = data.id;
  data.update_time = timestampToTime(data.update_time);
  console.log(data.update_time);

  const [form] = Form.useForm();
  useEffect(() => {
    form.setFieldsValue(data);
  }, []);

  const onFinish = (values: any) => {
    const dt = new Date(values.update_time); //删除里面的values.update_time 就变成当前时间
    values.id = dataid;
    values.update_time = dt.getTime() / 1000;
    console.log('Success:', values);
    dispatch({
      type: 'index/BIAN',
      payload: values,
    });
    history.push('/index/Home/Wq');
  };

  const onFinishFailed = (errorInfo: any) => {
    console.log('Failed:', errorInfo);
  };

  return (
    <div>
      <Form
        name="basic"
        form={form}
        labelCol={{ span: 8 }}
        wrapperCol={{ span: 8 }}
        initialValues={{ remember: true }}
        onFinish={onFinish}
        onFinishFailed={onFinishFailed}
        autoComplete="off"
      >
        <Form.Item label="楼盘名称" name="title">
          <Input />
        </Form.Item>
        <Form.Item label="均价" name="price">
          <Input />
        </Form.Item>
        <Form.Item label="楼盘地址" name="address">
          <Input />
        </Form.Item>
        <Form.Item label="售楼处手机号" name="sale_phone">
          <Input />
        </Form.Item>
        <Form.Item label="更新时间" name="update_time">
          <Input />
        </Form.Item>
        <Form.Item label="排名" name="hits">
          <Input />
        </Form.Item>

        <Form.Item wrapperCol={{ offset: 8, span: 8 }}>
          <Button type="primary" htmlType="submit">
            提交
          </Button>
        </Form.Item>
      </Form>
    </div>
  );
}

const mapStateToProps = () => ({});

export default connect(mapStateToProps)(Bianji);
